package com.jl.jlapp.mvp.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.os.StrictMode.VmPolicy.Builder;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import com.jl.jlapp.R;
import com.jl.jlapp.eneity.ImgUploadModel;
import com.jl.jlapp.mvp.base.BasePermActivity;
import com.jl.jlapp.mvp.base.BasePermActivity.PermissionCall;
import com.jl.jlapp.nets.Api;
import com.jl.jlapp.nets.CommonObserver;
import com.jl.jlapp.nets.Net;
import com.jl.jlapp.popu.PhotoPicPopupWindow;
import com.jl.jlapp.utils.FileUtil;
import com.jl.jlapp.utils.MimeTypeUtils;
import com.jl.jlapp.utils.PermissionTools;
import com.jl.jlapp.utils.ToastUtils;
import com.jl.jlapp.utils.Tools;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import java.io.File;
import java.sql.Date;
import java.text.SimpleDateFormat;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.MultipartBody.Part;
import okhttp3.RequestBody;

public class UserMessageSettingsActivity extends BasePermActivity {
    private static final String IMAGE_UNSPECIFIED = "image/*";
    private static final int NONE = 0;
    private static final int PHOTO_GRAPH = 1;
    private static final int PHOTO_RESOULT = 3;
    private static final int PHOTO_ZOOM = 2;
    @BindView(R.id.account_number)
    TextView accountNumber;
    Drawable drawable;
    File fi;
    @BindView(R.id.icon_return)
    ImageView iconReturn;
    OnClickListener itemclick = new OnClickListener() {
        public void onClick(View paramAnonymousView) {
            switch (paramAnonymousView.getId()) {
                case R.id.photographButton:
                    if (Build.VERSION.SDK_INT >= 24) {
                        StrictMode.setVmPolicy(new Builder().build());
                    }
                    if (PermissionTools.isCameraCanUse()) {
                        UserMessageSettingsActivity.this.picPopupWindow.dismiss();
                        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                        intent.putExtra("output", Uri.fromFile(UserMessageSettingsActivity.this.tempFile));
                        UserMessageSettingsActivity.this.startActivityForResult(intent, 1);
                        return;
                    }
                    ActivityCompat.requestPermissions(UserMessageSettingsActivity.this, new String[]{"android.permission.CAMERA"}, 1);
                    break;
                case R.id.photoButton:
                    UserMessageSettingsActivity.this.picPopupWindow.dismiss();
                    Intent intent = new Intent("android.intent.action.PICK", null);
                    intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                    UserMessageSettingsActivity.this.startActivityForResult(intent, 2);
                    break;
                case R.id.remove:
                    UserMessageSettingsActivity.this.picPopupWindow.dismiss();
                    break;
                default:
                    break;
            }

        }
    };
    @BindView(R.id.iv_avatar)
    ImageView ivAvatar;
    String mobile = "";
    String path = null;
    Bitmap photo;
    private PhotoPicPopupWindow picPopupWindow;
    String picUrl = "";
    SharedPreferences sharedPreferences;
    File tempFile = new File(Environment.getExternalStorageDirectory(), getPhotoFileName());
    int userId = 0;
    String userName = "";
    @BindView(R.id.user_name)
    TextView userNameTv;
    @BindView(R.id.user_pic_rela)
    RelativeLayout userPicRela;

    private String getPhotoFileName() {
        Date localDate = new Date(System.currentTimeMillis());
        SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("'IMG'_yyyyMMdd_HHmmss");
        return localSimpleDateFormat.format(localDate) + ".jpg";
    }

    private void refreshSDcard() {
        Intent localIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
        localIntent.setData(Uri.fromFile(this.fi));
        sendBroadcast(localIntent);
    }

    private void setPicToView(Intent paramIntent) {
        Bundle extras = paramIntent.getExtras();
        if (extras != null) {
            this.photo = ((Bitmap) extras.getParcelable("data"));
            this.drawable = new BitmapDrawable(null, this.photo);
            this.path = FileUtil.saveFile(this, this.tempFile.getName(), this.photo);
            Tools.setCircleIcon(this.path, this.ivAvatar);
        }
    }

    private void upAvatar(MultipartBody.Part paramPart) {
        Net.get().uploadPhoto(paramPart).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonObserver<ImgUploadModel>() {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(ImgUploadModel imgUploadModel) {
                        if (imgUploadModel.getCode()==200){
                            ToastUtils.show("修改头像成功！");
                        }
                    }
                });
    }

    protected int contentViewLayoutRes() {
        return R.layout.activity_user_message_settings;
    }

    public void onActivityResult(int paramInt1, int paramInt2, Intent paramIntent) {
        if (paramInt1 == 1) {
            File file = new File(Environment.getExternalStorageDirectory() + "/" + this.tempFile.getName());
            startPhotoZoom(Uri.fromFile(file));
        } else if (paramInt1 == 2) {
            startPhotoZoom(paramIntent.getData());
        } else if ((paramInt1 == 3) && (paramIntent.getExtras() != null)) {
            setPicToView(paramIntent);
            this.fi = new File(this.path);
            if (this.fi != null) {
                RequestBody localObject = RequestBody.create(MediaType.parse(MimeTypeUtils.getMimeType(this.fi)), this.fi);
                upAvatar(MultipartBody.Part.createFormData("file", this.fi.getName(), localObject));
            }
        }
        super.onActivityResult(paramInt1, paramInt2, paramIntent);
    }

    @OnClick({R.id.user_pic_rela, R.id.icon_return, R.id.user_name_settings_rela})
    public void onClick(View paramView) {
        switch (paramView.getId()) {
            case R.id.user_pic_rela:
                Tools.isFastClick(1000);
                PermissionCall call = new PermissionCall() {
                    public void refused() {
                        Toast.makeText(UserMessageSettingsActivity.this, "拒绝会导致拍照失败", Toast.LENGTH_SHORT).show();
                    }

                    public void requestSuccess() {
//                        UserMessageSettingsActivity.access$002(UserMessageSettingsActivity.this, new PhotoPicPopupWindow(UserMessageSettingsActivity.this, UserMessageSettingsActivity.this.itemclick));
                        UserMessageSettingsActivity.this.picPopupWindow.showAtLocation(UserMessageSettingsActivity.this.findViewById(R.id.my), 80, 0, 0);
                    }
                };
                requestRunTimePermissions(new String[]{"android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE"}, call);
                break;
            case R.id.icon_return:
                finish();
                break;
            case R.id.user_name_settings_rela:
                startActivity(new Intent(this, UserNameSettingActivity.class));
                break;
            default:
                break;
        }
    }

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        ButterKnife.bind(this);
        Tools.addActivity(this);
        this.sharedPreferences = getSharedPreferences("JLAppData", 0);
        this.picUrl = this.sharedPreferences.getString("userPicUrl", "userPicUrl");
        this.userId = this.sharedPreferences.getInt("userId", -1);
        this.mobile = this.sharedPreferences.getString("mobile", "");
        Tools.setCircleIcon("http://117.158.178.202:8001/JLMIS/" + this.picUrl, this.ivAvatar);
        picPopupWindow = new PhotoPicPopupWindow(this, itemclick);
        if (!"".equals(this.mobile)) {
            this.accountNumber.setText(Tools.hidePhone(this.mobile));
        } else {
            this.accountNumber.setText("暂无");
        }

    }

    protected void onResume() {
        super.onResume();
        this.userName = this.sharedPreferences.getString("userName", "");
        this.userNameTv.setText(this.userName);
    }

    public void startPhotoZoom(Uri paramUri) {
        Intent localIntent = new Intent("com.android.camera.action.CROP");
        localIntent.setDataAndType(paramUri, "image/*");
        localIntent.putExtra("crop", "true");
        localIntent.putExtra("aspectX", 1);
        localIntent.putExtra("aspectY", 1);
        localIntent.putExtra("outputX", 300);
        localIntent.putExtra("outputY", 300);
        localIntent.putExtra("return-data", true);
        startActivityForResult(localIntent, 3);
    }
}


/* Location:              /Users/davidzhang/Documents/decompileApk/jlfood-dex2jar.jar!/com/jl/jlapp/mvp/activity/UserMessageSettingsActivity.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */